package org.claaswilke.presents.ilp;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.junit.BeforeClass;
import org.junit.Test;

import de.claaswilke.presents.PresentGroup;
import de.claaswilke.presents.PresentsPackage;
import de.claaswilke.presents.resource.presents.mopp.PresentsResourceFactory;

public class IlpGeneratorTest {

	/**
	 * Returns the file object for a given path relative to the plug-in's
	 * directory.
	 * 
	 * @param path
	 *            The path of the resource.
	 * @param bundleId
	 *            The ID of the Bundle containing the resource.
	 * @return The found {@link File} object.
	 * @throws IOException
	 * @throws Exception
	 *             Thrown, if the opening fails.
	 */
	public static File getFile(String path, String bundleId) throws IOException {

		URL fileLocation;
		File file;

		if (Platform.isRunning()) {
			fileLocation = Platform.getBundle(bundleId).getResource(path);
			fileLocation = FileLocator.resolve(fileLocation);
			file = new File(fileLocation.getFile());
		}

		else {
			File testLocation = new File(".");
			file = new File(testLocation.getAbsolutePath() + File.separator
					+ path);
		}

		assertTrue(file.exists());

		return file;
	}

	@BeforeClass
	public static void setUp() throws IOException {

		if (!Platform.isRunning())
			registerEmfResourceFactories();
		// no else.
	}

	/**
	 * Registers EMF resource factories at EMF.
	 * 
	 * @throws IOException
	 */
	protected static void registerEmfResourceFactories() throws IOException {

		/* Probably register the present model resource for EMF. */
		if (Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().get(
				PresentsPackage.eNS_PREFIX) == null) {

			Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
					PresentsPackage.eNS_PREFIX, new PresentsResourceFactory());
		}
		// no else.

		if (EPackage.Registry.INSTANCE.getEPackage(PresentsPackage.eNS_URI) == null) {
			EPackage.Registry.INSTANCE.put(PresentsPackage.eNS_PREFIX,
					PresentsPackage.eINSTANCE);
		}
		// no else.
	}

	@Test
	public void test() throws IOException, InterruptedException {

		PresentGroup group = loadPresentGroup("christmas");
		IlpGenerator generator = new IlpGenerator();
		//System.out.println(generator.generateILP(group));
		generator.runILP(generator.generateILP(group));
	}

	protected PresentGroup loadPresentGroup(String modelName)
			throws IOException {
		File modelFile = getFile("resources/" + modelName + ".presents",
				"de.claaswilke.presents.resource.presents");

		ResourceSet rs1 = new ResourceSetImpl();
		Resource modelResource = rs1.createResource(URI.createFileURI(modelFile
				.getAbsolutePath()));
		modelResource.load(null);

		PresentGroup model = (PresentGroup) modelResource.getContents().get(0);

		return model;
	}
}
